package com.smartalarmclock.Logic;

import com.smartalarmclock.Logic.AcAutoResponseService.AcAutoResponseServiceStatic;
import com.smartalarmclock.Model.IProfile;
import com.smartalarmclock.Model.Exceptions.ConflictingSchedueledProfilesException;
import com.smartalarmclock.UI.AutoResponseSettingsActivity.GameToPlay;
import com.smartalarmclock.UI.Games.MathExerciseActivity;
import com.smartalarmclock.UI.Games.TextInputActivity;

import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;

public class AlarmClockBroadcastReceiver extends BroadcastReceiver {

	private static final String TAG = "AlarmClockBroadcastReceiver";
	
	@Override
	public void onReceive(Context context, Intent intent) {
		Log.v(TAG, "Alarm ringing!!!");
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
        wl.acquire();

        IProfile runningProfile = null;
        try {
        	runningProfile = AcManager.GetInstance().GetRunningProfile(context, true);
		} catch (ConflictingSchedueledProfilesException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        Class <?> gameToPlay = null;
        
        switch (runningProfile.GetAlarmClock().GetShutOffMethod())
        {
        case MathEquation:
        	gameToPlay = MathExerciseActivity.class;
        	break;
        case SentenceTyping:
        	gameToPlay = TextInputActivity.class;
        	break;
        case Snooze:
        	gameToPlay = null;
        }
        
        if (gameToPlay != null)
        {
			Intent gamesActivity = new Intent(context, gameToPlay);
		gamesActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(gamesActivity);
        }
        wl.release();	
        
        KeyguardManager keyguardManager = (KeyguardManager) context.getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE); 
        KeyguardLock keyguardLock =  keyguardManager.newKeyguardLock(getClass().getName());
        keyguardLock.disableKeyguard();
	}

}
